﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link rel="stylesheet" href="../assets/css/plugins/footable/footable.core.css" />
<link rel="stylesheet" href="../assets/css/shelf/addDisplayInfo.css" />
<link href="../assets/img/validate/validate.css" rel="stylesheet">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
<div class="addDisplayInfo">
	<form id="checkModuleForm">
	<div id="message" style="display: none"></div>
	<div class="panel panel-default">
		<div class="panel-heading">陈列基本信息</div>
		<div class="panel-body">
			<table class="base-info">
				<thead></thead>
				<tbody>
					<tr>
						<td>年份</td>
						<td>
							<select id="Year" class="form-control input-select"></select>
						</td>
						<td>季节</td>
						<td>
							<select id="Season" class="form-control input-select"></select>
						</td>
					</tr>
					<tr>
						<td>货架</td>
						<td>
							<input id="gsAuto" class="form-control input-text required" 
								datacol="yes" err="货架" checkexpession="NotNull"
								placeholder="在此输入货架名称或编号" data-provide="typeahead" />
							<input id="GSID" type="hidden" value="-1" />
						</td>
						<td>A级陈列标准SKC</td>
						<td>
							<input type="number" id="ASKC" placeholder="请输入A级陈列标准SKC" 
								datacol="yes" err="A级陈列标准SKC" checkexpession="NotNull"
								class="form-control input-text required" />
						</td>
					</tr>
					<tr>
						<td>B级陈列标准SKC</td>
						<td>
							<input type="number" id="BSKC" placeholder="请输入B级陈列标准SKC" 
								datacol="yes" err="B级陈列标准SKC" checkexpession="NotNull"
								class="form-control input-text required" />
						</td>
						<td>C级陈列标准SKC</td>
						<td>
							<input type="number" id="CSKC" placeholder="请输入C级陈列标准SKC" 
								datacol="yes" err="C级陈列标准SKC" checkexpession="NotNull"
								class="form-control input-text required" />
						</td>
					</tr>
				</tbody>
			</table>
        </div>
    </div>
    
    <div class="panel panel-default">
		<div class="panel-heading">陈列系列</div>
		<div class="panel-body">
			<table id="goodDetails" class="info-box footable table table-stripped">
				<thead>
					<tr>
						<td>编号</td>
						<td>系列</td>
						<td>商品层</td>
						<td>品类</td>
						<td>最低出样件数</td>
						<td>标准出样件数</td>
						<td>最高出样件数</td>
						<td>操作</td>
					</tr>
				</thead>
				<tbody>
					
				</tbody>
			</table>
        </div>
    </div>
    <div class="control">
    	<button type="button" class="btn btn-primary" id="save">保存</button>
    	<button type="button" class="btn btn-white closeBtn">关闭</button>     
    </div>
    </form>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
<script src="../assets/js/plugins/bootstrap-typeahead/bootstrap-typeahead.js"></script>
<script src="../assets/img/validate/learunui-validator.js"></script>
<script type="application/javascript">
	$(document).ready(function(){
		var SDID = Y.getUrlParam('id');
		var isCopy = Y.getUrlParam('isCopy');
		//初始化数据
		
		//绑定年份
		commonUtil.bindDict2({DictValue: 'year'},['#Year']);
            
		//绑定季节
		commonUtil.bindDict2({DictValue: 'season'},['#Season']);
		
		//初始化表格中的下拉框组
        // 1. el为新增的下拉框对象
        // 2. json为存储下拉框各项的集合
        var initSelectGroup = function(el,json,deal){
        	//判断是否创建新对象
        	if(el != null){
        		var save = false;
	        	var tempVal = '';
	        	json.map(function(item){
	        		el.append('<option value="'+ item.DictName +'">'+item.DictName+'</option>');
	        	});
        	}
        	//额外功能(按需调用)
        	if(!Y.checkNull(deal)){
        		deal();
        	}
        }
		
		//获取系列列表
		var styleJson = [];
		commonUtil.loadDict2({
			DictValue: 'style'
		},function(data){
			data.map(function(item){
				if(item.DictValue != 'style')
					styleJson.push(item);
			})
			initSelectGroup($('#goodDetails .Series:first'), styleJson, function(){
				if(styleJson.length == 0)
					$('#style .plus').hide();
			});
		});
		
		//获取品类列表
		var categoryJson = [];
		commonUtil.loadDict2({
			DictValue: 'category'
		},function(data){
			data.map(function(item){
				if(item.DictValue != 'category')
					categoryJson.push(item);
			})
			initSelectGroup($('#goodDetails .Category:first'), categoryJson, function(){
				if(categoryJson.length == 0)
					$('#category .plus').hide();
			});
		});
		
		//初始化表格每项的编号(el为 "#table tbody")
		var initTermNum = function(el){
			el.children().each(function(index){
				$(this).find('td.termNum').text(index + 1);
			})
		}
		
		Y.API2('DisplayShelf','GetID',{
			ID: SDID
		},function(data){
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				//初始化数据
				var count = $("#Year option").length;
			    for(var i=0; i<count; i++){
			    	if($("#Year").get(0).options[i].text == data.Year){
			            $("#Year").get(0).options[i].selected = true;
			            break;
			        }
			    }

			    count = $("#Season option").length;
			    for(var i=0; i<count; i++){
			    	if($("#Season").get(0).options[i].text == data.Season){
			            $("#Season").get(0).options[i].selected = true;
			            break;
			        }
			    }
				
				$('#gsAuto').val(data.GSName);
				$('#GSID').val(data.GSID);
				$('#ASKC').val(data.ASKC);
				$('#BSKC').val(data.BSKC);
				$('#CSKC').val(data.CSKC);
				
				if(data.GoodDetails.length > 0){
					data.GoodDetails.map(function(item){
						var tbody = $('#goodDetails tbody');			
						var html  = '<tr>';
							html += 	'<td class="termNum"></td>';
							html +=		'<td>';
							html +=			'<select class="Series form-control input-select" value="'+ item.Series +'"></select>';
							html +=		'</td>';
							html +=		'<td>';
							html +=			'<input maxlength="20" placeholder="请输入" class="CommodityLayer form-control input-text" value="'+ item.CommodityLayer +'" />';
							html +=		'</td>';
							html +=		'<td>';
							html +=			'<select class="Category form-control input-select" value="'+ item.Category +'"></select>';
							html +=		'</td>';
							html += 	'<td>';
							html +=			'<input type="number" placeholder="请输入" class="Minimum form-control input-number" value="'+ item.Minimum +'" />';
							html +=		'</td>';
							html += 	'<td>';
							html +=			'<input type="number" placeholder="请输入" class="Avgximum form-control input-number" value="'+ item.Avgximum +'" />';
							html +=		'</td>';
							html += 	'<td>';
							html +=			'<input type="number" placeholder="请输入" class="Maximum form-control input-number" value="'+ item.Maximum +'" />';
							html +=		'</td>';
							html +=		'<td>';
							html +=			'<input type="hidden" value="'+ item.SDDID +'" class="SDDID form-control input-text" />';
							html +=			'<i class="minus fa fa-minus"></i>';
							html +=			'<i class="plus fa fa-plus"></i>';
							html +=		'</td>';
							html += '</tr>';
						tbody.append(html);
						initTermNum(tbody);
						initTermBtn(tbody);
						initSelectGroup(tbody.find('.Series:last'), styleJson, function(){
							tbody.find('.Series:last').val(item.Series);
						});
						initSelectGroup(tbody.find('.Category:last'), categoryJson, function(){
							tbody.find('.Category:last').val(item.Category);
						});
					});
				}else{
					addNew($('#goodDetails tbody'));
				}
			}
		})
		
		//初始化表格每项的操作按钮(el为 "#table tbody")
		var initTermBtn = function(el){
			el.find('.plus').hide();
			el.find('tr:last .plus').show();
			
			if(el.children().size() == 1)
				el.find('.minus').hide();
			else
				el.find('.minus').show();
		}
		
		//自动填充
		$('#gsAuto').typeahead({
		    source: function (query, process) {
		        var parameter = {query: query};
		        $('#GSID').val(-1);
		        Y.API2('GoodShelf','List',{
		        	Name: $('#gsAuto').val()
		        },function(data){
		        	if(data.Success == false){
		        		swal(data.Message, '', 'error');
		        	}else{
		        		var results = data.Items.map(function(item){
		        			return JSON.stringify(item);
		        		})
		        		process(results);
		        	}
		        });
		    },
		    highlighter: function(item) {
		    	var item = JSON.parse(item);
         		return '名称：'+item.GSName+'　编号：'+item.GSCode;
      		},
			updater: function(item) {
				var item = JSON.parse(item);
				$('#GSID').val(item.GSID);
      			return item.GSName;
      		}
		});
			
		//陈列系列添加按钮
		$('#goodDetails').on('click','.plus',function(){
			addNew($(this).parents('tbody'));
		});
		
		//添加一行陈列系列
		var addNew = function(tbody){	
			var html  = '<tr>';
				html += 	'<td class="termNum"></td>';
				html +=		'<td>';
				html +=			'<select class="Series form-control input-select"></select>';
				html +=		'</td>';
				html +=		'<td>';
				html +=			'<input maxlength="20" placeholder="请输入" class="CommodityLayer form-control input-text" />';
				html +=		'</td>';
				html +=		'<td>';
				html +=			'<select class="Category form-control input-select"></select>';
				html +=		'</td>';
				html += 	'<td>';
				html +=			'<input type="number" placeholder="请输入" class="Minimum form-control input-number" />';
				html +=		'</td>';
				html += 	'<td>';
				html +=			'<input type="number" placeholder="请输入" class="Avgximum form-control input-number" />';
				html +=		'</td>';
				html += 	'<td>';
				html +=			'<input type="number" placeholder="请输入" class="Maximum form-control input-number" />';
				html +=		'</td>';
				html +=		'<td>';
				html +=			'<input type="hidden" value="0" class="SDDID form-control input-text" />';
				html +=			'<i class="minus fa fa-minus"></i>';
				html +=			'<i class="plus fa fa-plus"></i>';
				html +=		'</td>';
				html += '</tr>';
			tbody.append(html);
			initTermNum(tbody);
			initTermBtn(tbody);
			initSelectGroup(tbody.find('.Series:last'), styleJson);
			initSelectGroup(tbody.find('.Category:last'), categoryJson);
		}
		
		//表格删除按钮
		$('table').on('click','.minus',function(){
			var table = $(this).parents('table');
			var tbody = $(this).parents('tbody');
			var tr = $(this).parents('tr');
			swal({
				title: "确定要删除吗？",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定",
				cancelButtonText: "取消",
			}, function(){
				tr.remove();
				initTermNum(tbody);
				initTermBtn(tbody);
			});
		});
		
		//关闭当前标签页
        var closeActiveTab = function () {
			var closeBtn = $('.J_menuTab.active i', window.parent.document);
			closeBtn.click();
        }
        $('.closeBtn').click(function () {
        	swal({
				title: "确定要关闭当前页吗？",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定",
				cancelButtonText: "取消",
			}, function(){
				closeActiveTab();
			});
        });
        
        var checkSubmit = function(){
        	if (!CheckDataValid('#checkModuleForm')) {
                return false;
           	}
        	if(parseInt($('#GSID').val()) == -1){
        		swal({title:'没找到相关货架信息'},function(){
        			setTimeout(function(){
        				$('#gsAuto').focus();
        			},100);
        		});
    			return false;
        	}
        	var checkCommodity = true;
        	$('#goodDetails .CommodityLayer').each(function(index){
        		if($(this).val() == ''){
        			console.log(index);
        			$('#goodDetails tbody tr').eq(index).addClass('error');
        			checkCommodity = false;
        		}
        	});
        	if(!checkCommodity){
        		swal('适用商品层信息不能为空');
        		return false;
        	}
        	
        	var checkMinimum = true;
        	$('#goodDetails .Minimum').each(function(index){
        		if($(this).val() == ''){
        			$('#goodDetails tbody tr').eq(index).addClass('error');
        			checkMinimum = false;
        		}
        	});
        	if(!checkMinimum){
        		swal('最低出样件数不能为空');
        		return false;
        	}
        	
        	var checkAvgximum = true;
        	$('#goodDetails .Avgximum').each(function(index){
        		if($(this).val() == ''){
        			$('#goodDetails tbody tr').eq(index).addClass('error');
        			checkAvgximum = false;
        		}
        	});
        	if(!checkAvgximum){
        		swal('标准出样件数不能为空');
        		return false;
        	}
        	
        	var checkMaximum = true;
        	$('#goodDetails .Maximum').each(function(index){
        		if($(this).val() == ''){
        			$('#goodDetails tbody tr').eq(index).addClass('error');
        			checkMaximum = false;
        		}
        	});
        	if(!checkMaximum){
        		swal('最高出样件数不能为空');
        		return false;
        	}
        	
        	return true;
        }
        
    	//保存
    	$('#save').click(function(){
    		$('#goodDetails tr').removeClass('error');
    		
    		//非空判断
    		if(!checkSubmit())
    			return false;
    		
    		//校验数据是否重复
    		var checkRepeat = true;
    		var GoodDetails = [];
    		$('#goodDetails tbody tr').each(function(fstIndex){
    			if(!checkRepeat)
    				return false;
    			var series = $(this).find('.Series').val();
    			var commodityLayer = $(this).find('.CommodityLayer').val().trim();
    			var category = $(this).find('.Category').val();
    			var temp = {};
    			temp.SDDID = $(this).find('.SDDID').val();
    			temp.Series = series;
    			temp.CommodityLayer = commodityLayer;
    			temp.category = category;
    			temp.Minimum = $(this).find('.Minimum').val();
    			temp.Avgximum = $(this).find('.Avgximum').val();
    			temp.Maximum = $(this).find('.Maximum').val();
    			temp.Memo = '';
     			GoodDetails.push(temp);
     			
    			$('#goodDetails tbody tr').each(function(sndIndex){
    				//过滤自身
    				if(fstIndex != sndIndex){
    					if(series == $(this).find('.Series').val() && 
    						commodityLayer == $(this).find('.CommodityLayer').val().trim() && 
    							category == $(this).find('.Category').val()){
    						checkRepeat = false;
    						$('#goodDetails tbody tr').eq(fstIndex).addClass('error');
    						$('#goodDetails tbody tr').eq(sndIndex).addClass('error');
    						return false;
    					}
    				}
    			});
    		});
    		
    		if(!checkRepeat){
    			swal('存在重复的信息');
    			return false;
    		}
    		
    		var dealAction = Y.checkNull(isCopy) ? 'Modify' : 'Create';
    		
    		Y.API2('DisplayShelf', dealAction,{
    			SDID: SDID,
    			Year: $("#Year").find("option:selected").text(),
    			Season: $("#Season").find("option:selected").text(),
    			GSID: $('#GSID').val(),
    			ASKC: $('#ASKC').val(),
    			BSKC: $('#BSKC').val(),
    			CSKC: $('#CSKC').val(),
    			GoodDetails: GoodDetails
    		},function(data){
    			if(data.Success == false){
    				swal(data.Message, '', 'error');
    			}else{
    				swal({
						title: '保存成功',
						type: 'success'
					},function(){
						//刷新用户管理页
						if(window.parent.frames['iframe12'])
							window.parent.frames['iframe12'].document.getElementById('btn-search').click();
						
						//关闭当前页面
						closeActiveTab();
					});
    			}
    		})
    	});
	})
</script>
</asp:Content>

